package cn.com.analyze;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import org.jnetpcap.Pcap;
import org.jnetpcap.packet.JPacket;
import org.jnetpcap.packet.JPacketHandler;
import org.jnetpcap.protocol.network.Ip4;

import android.content.Context;
import android.util.Log;

public class Parser {
	private List<JPacket> packets;
	private Pcap pcap;
	
	Context c;
	
	TotalBytes tb;
	AverageHandshakeTime aht;
	
	public Parser(Context context)
	{
		c = context;
		packets = new ArrayList<JPacket>();
		tb = new TotalBytes(c);
		aht = new AverageHandshakeTime(c);
	}
	
	public void readPackets(String file)
	{
		StringBuilder builder = new StringBuilder();
		pcap = Pcap.openOffline(file, builder);
		if(pcap == null)
		{
			Log.e("Parser-readPackets", "pcap is null!");
			return;
		}
		JPacketHandler<Object> handler = new JPacketHandler<Object>(){
			//Ip4 ip4 = new Ip4();
			@Override
			public void nextPacket(JPacket packet, Object user) {
				// TODO Auto-generated method stub
				//packets.add(packet);
				//Log.i("Parser-readPackets", "---------read a packet-----------");
				
				tb.add(packet);
				aht.add(packet);
			}
			
		};
		
		pcap.loop(-1, handler, null);
		pcap.close();
		
		//startParser();
	}
	
	int totalBytes;
	
	public int getTotalSendBytes()
	{
		return tb.getTotalSend();
	}
	
	public int getTotalRecvBytes()
	{
		return tb.getTotalRecv();
	}
	
	public int getTotalBytes()
	{
		totalBytes = tb.getTotalBytes();
		return totalBytes;
	}
	
	public double getAHT()
	{
		return aht.getAverageHandshakeTime();
	}
}
